﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Dashboard
{
    public partial class MainForm : Form
    {
        string DashboardText = "";
        string button = "";

        int test1 = 1;
        int test2 = 1;

        DateTime currentTime;

        IniFiles ini;

        public MainForm()
        {
            this.StartPosition = FormStartPosition.CenterScreen;
            InitializeComponent();
        }


        private void MainForm_Load(object sender, EventArgs e)
        {

            ini = new IniFiles("setting.ini");
            currentTime = new DateTime();
        }

        private void 连接ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SingleCheck(sender);
            this.DashboardSerialPort.PortName = ini.ReadString("Serial", "com ", "COM7");
            this.DashboardSerialPort.BaudRate = ini.ReadInteger("Serial", "baudRate", 9600);
            this.DashboardSerialPort.Open();
            this.DashboardTimer.Enabled = true;
        }

        private void 断开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SingleCheck(sender);
            this.DashboardSerialPort.Close();
            this.DashboardTimer.Enabled = false;
        }
        private void SingleCheck(object sender)
        {
            连接ToolStripMenuItem.Checked = false;
            断开ToolStripMenuItem.Checked = false;
            ((ToolStripMenuItem)sender).Checked = true;
        }

        private void 配置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SettingForm sf = new SettingForm();
            sf.Show();
        }

        private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.baidu.com");
        }

        private void DashboardSerialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            try
            {
                string val = this.DashboardSerialPort.ReadLine();
                switch (int.Parse(val))
                {
                    case 1:
                        break;
                    case 2:
                        break;
                    case 3:
                        break;
                    case 4:
                        break;
                    case 5:
                        break;
                    case 6:
                        break;
                    case 7:
                        break;
                    case 8:
                        break;
                    default:
                        break;
                }
            }
            catch (Exception)
            {

                throw;
            } 
        }

        private void MainTimer_Tick(object sender, EventArgs e)
        {

            test1++;
            if (test1 > 10)
                test1 = 1;
            testData(test1);
        }


        private void testData(int data)
        {
            DashboardText = "     "+data.ToString()+"  ";
            //DashboardText = "   " + data.ToString()+"    ";
            //currentTime = System.DateTime.Now;
            //DashboardText = currentTime.ToString("HH.mm.ss.fff");
        }


        private void DashboardTimer_Tick(object sender, EventArgs e)
        {
            if (this.DashboardSerialPort.IsOpen)
            {
                this.DashboardSerialPort.WriteLine(DashboardText + "\n");
            }
            Console.WriteLine(DashboardText);
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.DashboardSerialPort.Close();
        }
    }
}
